﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace FrameWork.BasePage
{
    /// <summary>
    /// 引用自：Shivprasad koirala的文章
    /// http://www.codeproject.com/Articles/68371/Detecting-Refresh-or-Postback-in-ASP-NET
    /// </summary>
    public class SljaBasePage : System.Web.UI.Page, IHttpModule
    {
        Page _page = null;

        Queue<string> temp;
        public SljaBasePage()
        {
            temp = new Queue<string>();
        }
        public new void Init(HttpApplication context)
        {

            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }


        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {

            System.Web.HttpContext _httpContext = System.Web.HttpContext.Current;


            if (_httpContext != null)
            {
                _page = _httpContext.Handler as System.Web.UI.Page;

                if (_page != null)
                {
                    _page.Init += new EventHandler(_page_Init);
                    _page.Load += new EventHandler(_page_Load);
                }
                else
                {
                    return;
                }
            }
            else
            {
                return;
            }
        }

        void _page_Init(object sender, EventArgs e)
        {
            HiddenField hdnGuid = new HiddenField();
            hdnGuid.ID = "__hdnGuid";
            if (!_page.IsPostBack)
                hdnGuid.Value = Guid.NewGuid().ToString();
            _page.Form.Controls.Add(hdnGuid);
        }

        void _page_Load(object sender, EventArgs e)
        {
            HiddenField h1 = (HiddenField)(_page.Form.FindControl("__hdnGuid"));
            GuidClass currentGuid = new GuidClass();
            currentGuid.Guid = h1.Value;
            System.Web.HttpContext _httpContext = System.Web.HttpContext.Current;
           // if(temp.Contains(
            if (temp.Contains(currentGuid.Guid))
            {
                _httpContext.Items.Add("IsRefresh", true);
            }
            else
            {
                if (!(currentGuid.Guid.Equals(null) || currentGuid.Guid.Equals("")))
                    temp.Enqueue(currentGuid.Guid);
                _httpContext.Items.Add("IsRefresh", false);
            }

            //HtmlGenericControl script = new HtmlGenericControl("script");
            //script.Attributes["src"] = "/js/offen/CheckRefresh.js";
            //script.Attributes["type"] = "text/javascript";
            //_page.Header.Controls.Add(script);

        }
    }
    public class GuidClass
    {
        public GuidClass()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private string guid;

        public string Guid
        {
            get
            {
                return guid;
            }

            set
            {
                guid = value;
            }
        }
    }

     
} 


